package com.jinkaimen.kqb.ui.activity;

import android.app.Dialog;
import android.graphics.Color;
import android.os.Message;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.ht.baselibs.retrofit.NetCallBack;
import com.ht.baselibs.ui.base.BaseActivity;
import com.ht.baselibs.utils.ActivityUtils;
import com.ht.baselibs.utils.UIUtils;
import com.jinkaimen.kqb.R;
import com.jinkaimen.kqb.manager.ApiManager;
import com.jinkaimen.kqb.thread.ValidateThread;
import com.jinkaimen.kqb.thread.ValidateThread2;
import com.jinkaimen.kqb.ui.dialog.TipsDialog;
import com.jinkaimen.kqb.utils.ValidateUtils;

import retrofit2.Call;
import retrofit2.Response;

public class ResetPwdActivity extends BaseActivity {

    private EditText mTelView;
    private TextView mGetValidateBtn;
    private EditText mValidateView;
    private EditText mPwdView;
    private CheckBox mSeePwdView;
    private TextView mResetPwdVoiceSendCallTV;
    private TextView mResetPwdVoicecallTV;


    @Override
    protected boolean isStaticPage() {
        return true;
    }

    @Override
    protected void initTitleBar() {
        mTitleBarView.setTitleText("找回密码");
    }

    @Override
    protected int initContentView() {
        return R.layout.activity_reset_pwd;
    }

    @Override
    protected void initChildView() {
        mTelView = (EditText) findViewById(R.id.et_activity_register_tel);
        mGetValidateBtn = (TextView) findViewById(R.id.tv_activity_register_get_validate);
        mValidateView = (EditText) findViewById(R.id.et_activity_register_validate);
        mPwdView = (EditText) findViewById(R.id.et_activity_register_pwd);
        mSeePwdView = (CheckBox) findViewById(R.id.cb_activity_register_see_pwd);
        mResetPwdVoiceSendCallTV = (TextView) findViewById(R.id.tv_reset_pwd_voice_send_call);
        mResetPwdVoicecallTV = (TextView) findViewById(R.id.tv_reset_pwd_voice_call);
    }

    @Override
    protected void initChildData() {
        mResetPwdVoicecallTV.setVisibility(View.GONE);
    }

    @Override
    protected void setChildViewListener() {
        mSeePwdView.setOnClickListener(this);
        mGetValidateBtn.setOnClickListener(this);
        mResetPwdVoiceSendCallTV.setOnClickListener(this);
        findViewById(R.id.btn_activity_register).setOnClickListener(this);
        mResetPwdVoiceSendCallTV.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        super.onClick(v);
        String tel = mTelView.getText().toString();
        String code = mValidateView.getText().toString();
        String pwd = mPwdView.getText().toString();

        switch (v.getId()) {
            case R.id.tv_activity_register_get_validate:
                if (!ValidateUtils.isTelValidate(tel)) return;

                mGetValidateBtn.setEnabled(false);
                showLoadingDialogCanotCancle();
                ApiManager.getApi().sendValidate(
                        "HSS001005",
                        tel, "",
                        "2"
                ).enqueue(new NetCallBack<String>() {
                    @Override
                    protected void onSuccess(Call<String> call, Response<String> response) {
                        new ValidateThread(mHandler).start();
                        mGetValidateBtn.setTextColor(Color.parseColor("#bebebe"));
                    }

                    @Override
                    protected void hiddenProgress() {
                        hideLodingDialog();
                    }
                });
                break;
            case R.id.btn_activity_register:
                if (!ValidateUtils.isPwdValidate(pwd)) return;

                showLoadingDialog();
                ApiManager.getApi().resetPwd(
                        "HSY001004",
                        tel,
                        pwd,
                        code
                ).enqueue(new NetCallBack<String>() {
                    @Override
                    protected void onSuccess(Call<String> call, Response<String> response) {
                        UIUtils.showToast("重置密码成功");
                        ActivityUtils.finishActivity(ResetPwdActivity.this);
                    }

                    @Override
                    protected void hiddenProgress() {
                        hideLodingDialog();
                    }

                    @Override
                    protected boolean isNullNeedCallback() {
                        return true;
                    }
                });
                break;
            case R.id.cb_activity_register_see_pwd:
                if (mSeePwdView.isChecked()) {
                    mPwdView.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    mPwdView.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
            case R.id.tv_reset_pwd_voice_send_call:
                if (TextUtils.isEmpty(tel)) {
                    UIUtils.showToast("请输入手机号");
                    return;
                }
                if (tel.length() != 11) {
                    UIUtils.showToast("手机号格式错误,请重新输入");
                    return;
                }

                showLoadingDialog();
                ApiManager.getApi().sendVoice(
                        "HSY001064",
                        tel,
                        2
                ).enqueue(new NetCallBack<String>() {

                    private Dialog dialog;

                    @Override
                    protected void onSuccess(Call<String> call, Response<String> response) {
                        new ValidateThread2(mHandler).start();
                        mResetPwdVoiceSendCallTV.setTextColor(Color.parseColor("#bebebe"));
                        mResetPwdVoicecallTV.setVisibility(View.VISIBLE);
                        dialog = TipsDialog.SignleButtonDialog(ResetPwdActivity.this, "验证码将以电话形式通知到您，请注意收听", new View.OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                dialog.dismiss();
                            }
                        });
                        dialog.show();
                    }

                    @Override
                    protected void hiddenProgress() {
                        hideLodingDialog();
                    }

                });

                break;
        }
    }

    @Override
    protected void handleMessage(BaseActivity context, Message msg) {
        switch (msg.what) {
            case 100:
                mGetValidateBtn.setText(msg.obj.toString());
                break;
            case 200:
                mGetValidateBtn.setTextColor(Color.parseColor("#6284dd"));
                mGetValidateBtn.setText("获取验证码");
                mGetValidateBtn.setEnabled(true);
                break;
            case 300:
                mResetPwdVoiceSendCallTV.setText(msg.obj.toString());
                break;
            case 400:
                mResetPwdVoiceSendCallTV.setTextColor(Color.parseColor("#1a9a48"));
                mResetPwdVoiceSendCallTV.setText("使用语音验证码");
                mResetPwdVoiceSendCallTV.setEnabled(true);
                break;
        }
    }
}
